/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ekuma.impexp;

import cn.ekuma.util.PropHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class ImpExpConfig {
    private static String FileName="ImpExpConfig";
    private static String PROPERTY_table_model="table_model";//导入导出格式模版名称
    private static String PROPERTY_defaultEngine="defaultEngine";//默认导入导出方式
    private static String PROPERTY_defaultDataDir="defaultDataDir";//默认导入导出数据存放文件夹。

    private static Logger logger = Logger.getLogger("cn.ekuma.impexp.ImpExpConfig");

    private static Properties m_propsconfig;
    private File configfile;

    private Map<String,Map<String,String>> tableFieldGenerator=new HashMap<String,Map<String,String>>();
    private Map<String,Map<String,String>> tableFieldConverses=new HashMap<String,Map<String,String>>();

    public ImpExpConfig(File configfile) {
        init(configfile);
    }

    public ImpExpConfig() {
        init(getDefaultConfig());
    }

    private void init(File configfile) {
        this.configfile = configfile;
        m_propsconfig = new Properties();

        logger.info("Reading configuration file: " + configfile.getAbsolutePath());
    }

    private File getDefaultConfig() {
        return new File(new File(System.getProperty("user.home")), ImpExpConfig.FileName+ ".properties");
    }

    public  String getProperty(String sKey) {
        return m_propsconfig.getProperty(sKey);
    }

    public File getConfigFile() {
        return configfile;
    }

    public void setProperty(String sKey, String sValue) {
        if (sValue == null) {
            m_propsconfig.remove(sKey);
        } else {
            m_propsconfig.setProperty(sKey, sValue);
        }
    }

    public boolean delete() {
        loadDefault();
        return configfile.delete();
    }

    public void load() {
        loadDefault();
        try {
            InputStream in = PropHelper.guessPropFile(ImpExpConfig.class,configfile.getName());
            if (in != null) {
                m_propsconfig.load(in);
                in.close();
            }
        } catch (IOException e){
            loadDefault();
        }
    }

    public void save() throws IOException {
        OutputStream out = new FileOutputStream(configfile);
        if (out != null) {
            m_propsconfig.store(out, ImpExpConfig.FileName + ".properties");
            out.close();
        }
    }

    private void loadDefault() {
        m_propsconfig = new Properties();
    }

    public String getTableEngineProperty(String engineStyle,String property){
        return null;
    }

    public boolean getTableEngineBooleanProperty(String engineStyle,String property){
        return false;
    }

    public String getTableFieldConverse(String table,String field){
        return null;
    }

    public String getTableFieldGenerator(String table,String field){
        return null;
    }
}
